﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using PTTK19.BUS;

namespace PTTK19.GUI
{
    public partial class QuanLySanPham : Form
    {
        public QuanLySanPham()
        {
            InitializeComponent();
        }

        private void QuanLySanPham_Load(object sender, EventArgs e)
        {
            HienThiDataGridview();
            HienThiCmbLoaiSP();
            HienThiCmbNguyenLieu();
            btnDongY.Visible = false;
        }
        public void HienThiDataGridview()
        {
            dgvSanPham.DataSource = BUS_SanPham.LayThongTinChiTietSanPham();
        }
        public void HienThiCmbLoaiSP()
        {
            cmbLoaiSP.DataSource = BUS_SanPham.LayMaLoaiSanPham();
            cmbLoaiSP.ValueMember = "MaLoaiSP";
            cmbLoaiSP.DisplayMember = "TenLoaiSP";
        }
        public void HienThiCmbNguyenLieu()
        {
            cmbNguyenLieu.DataSource = BUS_NguyenLieu.LayMaNguyenLieu();
            cmbNguyenLieu.ValueMember = "MaNguyenLieu";
            cmbNguyenLieu.DisplayMember = "TenNguyenLieu";
        }

        private void btnThemSP_Click(object sender, EventArgs e)
        {
            btnDongY.Visible = true;
            btnThemSP.Visible = false;
            btnXoaSP.Enabled = false;
            btnCapNhatSP.Enabled = false;
            
        }

        private void btnXoaSP_Click(object sender, EventArgs e)
        {
            if (lblMaSP.Text == "")
            {
                MessageBox.Show("Hãy chọn sản phẩm muốn xóa.");
            }
            else
            {
                int Masp = 0;
                Masp = int.Parse(lblMaSP.Text);
                int iKQ = 0;
                iKQ = BUS_SanPham.XoaSanPham(Masp);
                if (iKQ > 0)
                {
                    MessageBox.Show("Xóa sản phẩm thành công.");
                    dgvSanPham.DataSource = null;
                    HienThiDataGridview();
                    lblMaSP.Text = "";
                    txtTenSP.Text = "";
                    txtDVT.Text = "";
                }
                else
                {
                    MessageBox.Show("Xóa sản phẩm thất bại.");
                }
            }
        }

       

        private void btnCapNhatSP_Click(object sender, EventArgs e)
        {
            BUS_SanPham sanpham = new BUS_SanPham();
            sanpham.MaSP = int.Parse(lblMaSP.Text);
            sanpham.TenSP = txtTenSP.Text;
            sanpham.DonVT = txtDVT.Text;
            sanpham.MaLoaiSP = int.Parse(cmbLoaiSP.SelectedValue.ToString());
            sanpham.MaNguyenLieu = int.Parse(cmbNguyenLieu.SelectedValue.ToString());
            string a = cmbTinhTrang.SelectedIndex.ToString(); ;
            if (a == "-1")
            {
                MessageBox.Show("Chưa chọn tình trạng");
                sanpham.TinhTrang = int.Parse(a);
            }
            else
            {
                int iKQ = 0;
                iKQ = BUS_SanPham.CapNhatSanPham(sanpham);
                if (iKQ > 0)
                {
                    MessageBox.Show("Cập nhật sản phẩm thành công.");
                    dgvSanPham.DataSource = null;
                    HienThiDataGridview();
                }
                else
                {
                    MessageBox.Show("Cập nhật sản phẩm thất bại.");
                }
            }
        }

        private void dgvSanPham_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int indexrow = e.RowIndex;
            DataGridViewRow row = dgvSanPham.Rows[indexrow];
            lblMaSP.Text = row.Cells[0].Value.ToString();
            txtTenSP.Text = row.Cells[1].Value.ToString();
            txtDVT.Text = row.Cells[2].Value.ToString();
            int index = int.Parse(row.Cells[3].Value.ToString());
            cmbLoaiSP.SelectedValue = index;
            index = int.Parse(row.Cells[4].Value.ToString());
            cmbNguyenLieu.SelectedValue = index;
            index = int.Parse(row.Cells[5].Value.ToString());
            cmbTinhTrang.SelectedIndex = index;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (txtTenSP.Text == "" || txtDVT.Text == "")
            {
                MessageBox.Show("Hãy điền đầy đủ thông tin.");
            }
            else
            {
                BUS_SanPham sanpham = new BUS_SanPham();
                sanpham.MaSP = 1;
                sanpham.TenSP = txtTenSP.Text;
                sanpham.DonVT = txtDVT.Text;
                sanpham.MaLoaiSP = int.Parse(cmbLoaiSP.SelectedValue.ToString());
                sanpham.MaNguyenLieu = int.Parse(cmbNguyenLieu.SelectedValue.ToString());
                string a = cmbTinhTrang.SelectedIndex.ToString(); ;
                if (a == "-1")
                {
                    MessageBox.Show("Chưa chọn tình trạng");
                    sanpham.TinhTrang = int.Parse(a);
                }
                else
                {
                    int iKQ = 0;
                    iKQ = BUS_SanPham.ThemSanPham(sanpham);
                    if (iKQ > 0)
                    {
                        MessageBox.Show("Thêm sản phẩm thành công.");
                        dgvSanPham.DataSource = null;
                        HienThiDataGridview();
                    }
                    else
                    {
                        MessageBox.Show("Thêm sản phẩm thất bại.");
                    }
                }
            }
            btnDongY.Visible = false;
            btnThemSP.Visible = true;
            btnXoaSP.Enabled = true;
            btnCapNhatSP.Enabled = true;
        }

       
               
     

    }
}
